package com.vimaltuts.mycontacts;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class SQLiteConnector {
	
	private SQLiteDatabase db;
	private SQLiteHelper sqlHp;
	private Cursor cur;

	public SQLiteConnector(Context context) {
		sqlHp = new SQLiteHelper(context, Contacts.DB_NAME, null, 1);
	}

	public void insertContact(String name, String phone, String mail,String fb,byte[] blob) {
		ContentValues cv = new ContentValues();
		cv.put(Contacts.NAME, name);
		cv.put(Contacts.PHONE, phone);
		cv.put(Contacts.MAIL, mail);
		cv.put(Contacts.FB, fb);
		cv.put(Contacts.IMAGE,blob);

		db = sqlHp.getWritableDatabase();
		db.insert(Contacts.TABLE, null, cv);
		db.close();
	}

	public void updateContact(long id,String name, String phone, String mail,String fb,byte[] blob) {
		ContentValues cv = new ContentValues();
		cv.put(Contacts.NAME, name);
		cv.put(Contacts.PHONE, phone);
		cv.put(Contacts.MAIL, mail);
		cv.put(Contacts.FB, fb);
		cv.put(Contacts.IMAGE,blob);

		db = sqlHp.getWritableDatabase();
		db.update(Contacts.TABLE, cv, Contacts.ID+"="+ id, null);
		db.close();
	}

	public Cursor getAllContacts() {
		db = sqlHp.getReadableDatabase();
		cur=db.query(Contacts.TABLE,null, null,null, null, null, "name");
		return cur;
	}

	public Cursor getOneContact(long id) {
		db = sqlHp.getReadableDatabase();
		cur=db.query(Contacts.TABLE, null, "_id="+ id, null, null, null,null);
		return cur;
	}

	public void deleteContact(long id) {
		System.out.println("DELETE ");
		db = sqlHp.getWritableDatabase();
		db.delete(Contacts.TABLE, "_id="+id , null);
		db.close();
	}
}
